package com.food.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.food.dao.service.ApplicationService;
import com.food.dao.service.CollectService;
import com.food.dao.service.CourseService;
import com.food.mail.Mail_Test;
import com.food.model.Application;
import com.food.model.Collect;
import com.food.model.Course;
import com.food.model.Member;

/**
 * Servlet implementation class ApplyCourseServlet
 */
@WebServlet("/ApplyCourseServlet")
public class ApplyCourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ApplyCourseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {

			String mbid = request.getParameter("mbid");
			String cid1 = request.getParameter("cid");

			int cid = Integer.parseInt(request.getParameter("cid"));

			HttpSession session = request.getSession();

			Member member = (Member) session.getAttribute("user");

			Application application = new Application();
			application.setCid(cid);
			application.setMbid(member.getMbid());

			ApplicationService appService = new ApplicationService();

			List<Application> list = appService
					.findApplicationByMb(application);
			for (Application cl : list) {
				if (cl.getCid() == cid) {
					request.setAttribute("fail", "fail");
					RequestDispatcher rd = request
							.getRequestDispatcher("/course/application.jsp?cid="
									+ cid);
					rd.forward(request, response);
					return;
				}

			}

			appService.insertApplication(application);
			request.setAttribute("success", "success");
			
			CourseService courseService = new CourseService();
			Course course = new Course();
			course.setCid(cid);
			course = courseService.findOneCourse(course);
			try{
			Mail_Test.send("yj", member, course);
			request.setAttribute("mail", "mail");
			RequestDispatcher rd = request
					.getRequestDispatcher("/course/DisplayCourse");
			rd.forward(request, response);
			return;
			}
			catch(Exception e){
				RequestDispatcher rd = request
						.getRequestDispatcher("/course/DisplayCourse");
				rd.forward(request, response);
				return;
				
			}
//			String contextPath = getServletContext().getContextPath();
//			response.sendRedirect(response.encodeRedirectURL(contextPath+"/course/DisplayCourse"));
			
			

		} catch (Exception e) {

			RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
			rd.forward(request, response);
			return;
		}
	}

}
